package com.bdth.bdthiss.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.bdth.bdthiss.entity.Brand;
import com.bdth.bdthiss.entity.Brands;
import com.bdth.bdthiss.service.BrandService;
import com.bdth.bdthiss.service.BrandsService;
import com.bdth.bdthiss.vo.MessageResult;
import com.bdth.bdthiss.vo.PageVo;
import com.bdth.bdthiss.vo.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2020/4/9.
 */
@Controller
@RequestMapping("/brand")
public class BrandController extends BaseController {

    @Autowired
    private BrandsService brandsService;

    @Autowired
    private BrandService brandService;

    @RequestMapping("index")
    public String index(){
        return "brand/index";
    }

    @RequestMapping("search")
    public String search(){
        return "brand/search";
    }

    @RequestMapping("form")
    public String form(){
        return "brand/form";
    }

    @RequestMapping(value = "list")
    @ResponseBody
    public PageVo<Brands> getPageList(HttpServletRequest request, Pagination pagination,
                                     @RequestParam(required = false) String name){
        Map<String ,Object> query = new HashMap<>();
        //查询条件
        if(name!=null && !name.equals(""))
            query.put("name",name);
        Page<Brands> items = brandsService.listPages(pagination, query);
        PageVo<Brands> pageVo = new PageVo<>(items.getRecords(),items.getTotal());
        return pageVo;
    }

    @RequestMapping(value = "sync")
    @ResponseBody
    public MessageResult syncValue(){
        EntityWrapper<Brand> entityWrapper = new EntityWrapper<>();
        List<Brand> list = brandService.selectList(entityWrapper);
        EntityWrapper<Brands> bew = new EntityWrapper<>();
        brandsService.delete(bew);
        for (Brand bean:list) {
            Brands temp = new Brands();
            temp.setBrandid(bean.getId());
            temp.setCnname(bean.getName());
            brandsService.insert(temp);
        }
        return MessageResult.getSuccess("同步数据成功");
    }

    @RequestMapping(value = "save")
    @ResponseBody
    public MessageResult save(Brands param){
        int brandid = insertBrand(param.getCnname());
        param.setBrandid(brandid);
        brandsService.insert(param);
        return MessageResult.getSuccess("添加成功");
    }

    /**
     * 添加新的品牌信息
     */
    public int insertBrand(String brandname){
        EntityWrapper<Brand> entityWrapper = new EntityWrapper<>();
        entityWrapper.eq("name",brandname);
        List<Brand> brandList = brandService.selectList(entityWrapper);
        if(brandList!=null && brandList.size()>0)
            return brandList.get(0).getId();
        Brand bean = new Brand();
        bean.setName(brandname);
        bean.setHash(0);
        bean.setSort(0);
        brandService.insert(bean);
        return bean.getId();
    }

    @RequestMapping(value = "update")
    @ResponseBody
    public MessageResult update(Brands param){
        Brand bean = new Brand();
        bean.setId(param.getBrandid());
        String name = "";
        if(param.getCnname()!=null&&!"".equals(param.getCnname())){
            name += param.getCnname();
        }
        if(param.getEnname()!=null&&!"".equals(param.getEnname())){
            name += "/"+param.getEnname();
        }
        bean.setName(name);
        brandService.updateById(bean);
        brandsService.updateById(param);
        return MessageResult.getSuccess("修改成功");
    }

    @RequestMapping(value = "delete")
    @ResponseBody
    public MessageResult delete(@RequestParam Integer ids){
        EntityWrapper<Brands> e = new EntityWrapper<>();
        e.eq("brandid", ids);
        brandsService.delete(e);
        brandService.deleteById(ids);
        return MessageResult.getSuccess("数据已删除");
    }
}
